package servlet;

import com.google.gson.Gson;
import com.mysql.cj.xdevapi.JsonArray;
import news.example.Topic;
import news.service.IQueryTopicService;
import news.service.IQueryUserService;
import news.service.QueryTopicServiceImpl;
import news.service.QueryUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Administrator
 * 添加主题
 */
@WebServlet("/querySelTopic")
public class QueryTopicController extends HttpServlet {

    private IQueryTopicService iQueryTopicService = new QueryTopicServiceImpl();
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");

        List<Topic> list = iQueryTopicService.queryAllTopic();

        List<String> nameList = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            nameList.add(list.get(i).getTopicName());
        }


        Gson gson = new Gson();
        String json = gson.toJson(nameList);

        PrintWriter out = response.getWriter();
        out.write(json);
    }

}